package com.itheima.lj.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.lj.R;
import com.itheima.lj.app.AppContext;
import com.itheima.lj.base.BaseListAdapter;
import com.itheima.lj.bean.News;

import static android.R.id.list;

/**
 * Created by Administrator on 2017/9/3 0003.
 */
public class NewsAdapter extends BaseListAdapter<News> {
    //创建Holder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //初始化列表条目
        View itemView=LayoutInflater.from(AppContext.getContext()).inflate(R.layout.item_news,parent,false);
        return new NewsHolder(itemView);
    }
    //绑定数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        NewsHolder newsHolder= (NewsHolder) holder;
        newsHolder.tv.setText(list.get(position).getTitle());
        newsHolder.tv.setTextColor(Color.RED);
        //对列表条目设置点击监听
        newsHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AppContext.getContext(), list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    //自定义资讯列表的ViewHolder
    static  class NewsHolder extends  RecyclerView.ViewHolder{
        TextView tv;
        public NewsHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}
